/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.servlet;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.dao.PostDAO;
import model.dao.ProjectDAO;
import model.dao.ProjectTypeDAO;
import model.dao.ProjectUserDAO;
import model.dao.UserDAO;
import model.dao.service.PostDAOService;
import model.dao.service.ProjectDAOService;
import model.dao.service.ProjectTypeDAOService;
import model.dao.service.ProjectUserDAOService;
import model.dao.service.UserDAOService;
import model.entities.Post;
import model.entities.Project;
import model.entities.ProjectType;
import model.entities.User;
import util.Constants;

/**
 *
 * @author HAI VU
 */
public class Page extends HttpServlet {

    ProjectTypeDAOService TYPE_SERVICE = ProjectTypeDAO.getInstance();
    ProjectDAOService PROJECT_SERVICE = ProjectDAO.getInstance();
    ProjectUserDAOService PU_SERVICE = ProjectUserDAO.getInstance();
    UserDAOService USER_SERVICE = UserDAO.getInstance();
    PostDAOService POST_SERVICE = PostDAO.getInstance();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = request.getParameter("p");
        switch (page) {
            case "login":
                request.setAttribute(util.Constants.PAGE, "login");
                request.removeAttribute(util.Constants.MSG_RESULT);
                request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                request.getSession().removeAttribute(Constants.MSG_RESULT);
                break;
            case "logout":
                HttpSession session = request.getSession();
                session.invalidate();
                response.sendRedirect(util.Constants.URL_INDEX);
                break;
            case "signup":
                request.setAttribute(util.Constants.PAGE, "signup");
                //   request.getSession().removeAttribute(Constants.MSG_RESULT);
                request.removeAttribute(util.Constants.MSG_RESULT);
                request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                break;
            case "team":
                request.setAttribute(util.Constants.PAGE, "team");
                request.removeAttribute(util.Constants.MSG_RESULT);
                request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                break;
            case "result":
                request.setAttribute(util.Constants.PAGE, "result");
                request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                break;
            case "home":
                try {
                    List<Post> post = POST_SERVICE.getListPost();
                    request.setAttribute("newpost", post);
                    request.setAttribute(util.Constants.PAGE, null);
                    request.removeAttribute(util.Constants.MSG_RESULT);
                    request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                } catch (Exception ex) {
                    Logger.getLogger(AuthencationServlet.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            case "profile":
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "profile");
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "admin":
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "admin");
                request.getRequestDispatcher(util.Constants.URL_ADMIN).forward(request, response);
                break;
            case "newpost":
                request.setAttribute(util.Constants.PAGE, "newpost");
                request.removeAttribute(util.Constants.MSG_RESULT);
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "update":
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "update");
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "avatar":
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "avatar");
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "changepass":
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "changepass");
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "listpost":
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "listpost");
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "contact":
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "contact");
                request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                break;
            case "list_post":
                int categories = Integer.valueOf(request.getParameter("c"));
                try {
                    List<Post> post = POST_SERVICE.getListPostByCategories(categories);
                    request.removeAttribute(util.Constants.PAGE);
                    request.removeAttribute(util.Constants.MSG_RESULT);
                    request.setAttribute("newpost", post);
                    if (categories == 1) {
                        request.setAttribute("titleCat", "Java");
                    } else if (categories == 2) {
                        request.setAttribute("titleCat", ".Net");
                    } else if (categories == 3) {
                        request.setAttribute("titleCat", "PHP");
                    } else if (categories == 4) {
                        request.setAttribute("titleCat", "XML");
                    } else {
                        request.setAttribute("titleCat", "HTML");
                    }
                    request.setAttribute(util.Constants.PAGE, "list_post");
                    request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                } catch (Exception ex) {
                    Logger.getLogger(AuthencationServlet.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            case "project":
                User currentUser = (User) request.getSession().getAttribute(util.Constants.CURRENT_USER);
                List<Project> projectList = PU_SERVICE.getProjectByUser(currentUser.getUserID());
                request.setAttribute(util.Constants.PROJECT_LIST, projectList);
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "project");
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "create_project":
                List<ProjectType> typeList = TYPE_SERVICE.getTypes();
                request.removeAttribute(util.Constants.PAGE);
                request.setAttribute(util.Constants.PAGE, "create_project");
                request.setAttribute("typeList", typeList);
                request.getRequestDispatcher(util.Constants.URL_MEMBER).forward(request, response);
                break;
            case "post_view":
                int id = Integer.valueOf(request.getParameter("id"));
                try {
                    Post post = POST_SERVICE.getPostByID(id);
                    request.removeAttribute(util.Constants.PAGE);
                    request.removeAttribute(util.Constants.MSG_RESULT);
                    request.setAttribute("post", post);
                    request.setAttribute(util.Constants.PAGE, "post_view");
                    request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                } catch (Exception ex) {
                    Logger.getLogger(AuthencationServlet.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
